package org.hypirinha.utils.html2hypirinha;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.ParserConfigurationException;

import org.dom4j.Document;
import org.dom4j.io.DOMReader;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.DomSerializer;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;

public class Cleaner {

	public Document clean(InputStream in) throws IOException {
        HtmlCleaner cleaner = new HtmlCleaner();

        CleanerProperties props = cleaner.getProperties();
        props.setOmitDoctypeDeclaration(true);
        props.setNamespacesAware(false);

        TagNode node = cleaner.clean(in);
        
        try {
			return new DOMReader().read(new DomSerializer(props).createDOM(node));
		} catch (ParserConfigurationException e) {
			throw new RuntimeException(e);
		}
    }
}